# -*- encoding:utf-8 -*-

import unittest
import simplejson

from taobao.TaobaoApiException import TaobaoApiException
from taobao.TaobaoJsonRestClient import TaobaoJsonRestClient
from taobao.model.Item import Item
from taobao.model.ItemSearch import ItemSearch

from taobao.request.ItemGetRequest import ItemGetRequest
from taobao.request.ItemsGetRequest import ItemsGetRequest
from taobao.request.ItemsSearchRequest import ItemsSearchRequest

from taobao.response.ItemGetResponse import ItemGetResponse
from taobao.response.ItemsGetResponse import ItemsGetResponse
from taobao.response.ItemsSearchResponse import ItemsSearchResponse

class ItemApiTest(unittest.TestCase):
    """unit test for Item APIs"""

    def setUp(self):
        self.client = TaobaoJsonRestClient("http://gw.api.tbsandbox.com/router/rest", "test", "test")
        
    def testItemGet(self):
        request = ItemGetRequest()
        self.failUnless( (True == isinstance(request, ItemGetRequest) ), "check ItemGetRequest type")

        request.setFields("title,approve_status,iid,pic_path")
        request.setIid("041e5fcf953382825c14bc7fa541dcf2")
        request.setNick("alipublic28")
        
        try:
            response = self.client.ItemGet(request)
        except TaobaoApiException, e:
            print "ERROR:", e.code, ", Message:", e.msg
            return False
        
        self.assertTrue(isinstance(response, ItemGetResponse))

        print response.getBody()

        item = response.getItem()
        self.failUnless( (True == isinstance(item, Item) ), "check Item type")
        self.assertEqual(item.getIid(), request.iid, "check Item.iid")        

    def testItemsGet(self):
        request = ItemsGetRequest()
        self.failUnless( (True == isinstance(request, ItemsGetRequest) ), "check ItemsGetRequest type")

        request.setFields("iid,title,nick,pic_path,cid,price,type,delist_time,post_fee")
        request.setQ("mp3")
        #request.setNicks("hz0799")
        
        try:
            response = self.client.ItemsGet(request)
        except TaobaoApiException, e:
            print "ERROR:", e.code, ", Message:", e.msg
            return False
        
        self.assertTrue(isinstance(response, ItemsGetResponse))

        body = simplejson.loads(response.getBody(), encoding='UTF-8')
        #print simplejson.dumps(body,sort_keys=True, indent=4)
        print response.getTotalResults()
        
        for item in response.getItems():
            self.failUnless( (True == isinstance(item, Item) ), "check Item type")
            print item.getTitle()

    def testItemsSearch(self):
        request = ItemsSearchRequest()
        self.failUnless( (True == isinstance(request, ItemsSearchRequest) ), "check ItemsSearchRequest type")

        request.setFields("title,nick,price")
        request.setQ("mp3")
        
        try:
            response = self.client.ItemsSearch(request)
        except TaobaoApiException, e:
            print "ERROR:", e.code, ", Message:", e.msg
            return False
        
        self.assertTrue(isinstance(response, ItemsSearchResponse))

        body = simplejson.loads(response.getBody(), encoding='UTF-8')
        #print simplejson.dumps(body,sort_keys=True, indent=4)
        print response.getTotalResults()
        
        for search in response.getSearch():
            self.failUnless( (True == isinstance(search, ItemSearch) ), "check ItemSearch type")
            print search.getCategoryList()

    def tearDown(self):
        pass

def suite():
    itemTestSuite = unittest.TestSuite()
    itemTestSuite.addTest(ItemApiTest("testItemGet"))
    itemTestSuite.addTest(ItemApiTest("testItemsGet"))
    itemTestSuite.addTest(ItemApiTest("testItemsSearch"))    
    return itemTestSuite

if __name__ == "__main__":
    unittest.main(defaultTest='suite')
